package com.xywy.common.net;

import android.content.Context;
import android.os.AsyncTask;

import com.xywy.common.constants.ErrorCode;
import com.xywy.common.util.StringUtils;

/**
 * Created by xgd on 2014/6/13.
 */
public class MyAsyncTask extends AsyncTask<Parser, Integer, Object> {

    private static String TAG = "NetWorkTask";

    public MyAsyncTask() {
    }

    /**
     * 预处理动作
     */
    @Override
    protected void onPreExecute() {


        //过滤多次请求
        if (paramsParser.filter) {
            if (ParserManager.getInstance().contain(paramsParser)) {
                return;
            }
            ParserManager.getInstance().add(paramsParser);
        }

        super.onPreExecute();
    }


    //异步执行task
    private NetWorkTask netWorkTask;
    //请求parser封装参数
    private Parser paramsParser;
    //返回Json字符串
    private Parser resultParser;

    //结果的监听回调
    private OnResultListenerAdapter onResultListenerAdapter;

    /**
     * 执行网络请求
     *
     * @param params
     * @return 封装parser
     */
    @Override
    protected Object doInBackground(Parser... params) {


        //获得异步请求网络实例
        netWorkTask = NetWorkTask.getInstance();

        //执行网络请求
        resultParser = netWorkTask.client(params[0]);

        return resultParser;
    }


    /**
     * 执行各种回调
     *
     * @param result
     */
    @Override
    protected void onPostExecute(Object result) {

        //关掉所有进度条,对话框
        PromptManager.closeProgressDialog();
        if (ParserManager.getInstance().contain(paramsParser)) {

            ParserManager.getInstance().remove(paramsParser);
        }

        if (this.onResultListenerAdapter != null) {
            if (!StringUtils.isNull(((Parser) result).toString())) {
                //请求成功
                onResultListenerAdapter.onSuccess(((Parser) result), ((Parser) result).getResult());
            } else {
                onResultListenerAdapter.onNoNetWork(paramsParser, ErrorCode.ERR_NO_NETWORK);
            }


        }
    }


    //请求上下文
    private Context context;

    /**
     * 执行网络请求代理。
     * 可进行预处理操作：判断网络请求、显示进度条、弹Toast
     *
     * @param parser
     * @return
     */
    public final AsyncTask<Parser, Integer, Object> executeProxy(Parser parser, OnResultListenerAdapter onResultListenerAdapter) {

        if (parser != null) {
            this.paramsParser = parser;
            if (onResultListenerAdapter instanceof OnResultListener || onResultListenerAdapter instanceof OnResultListener) {

                this.onResultListenerAdapter = onResultListenerAdapter;

                //获取Context
                this.context = paramsParser.context;

                //检查网络，isMobile||isWLAN,没有判断APN网路连接
                if (NetWorkUtil.checkNetWork(context)) {

                    if (paramsParser.isShowProgress) {
                        PromptManager.showProgressDialog(context);
                        return null;
                    }

                    //执行父类execute
                    return super.execute(paramsParser);

                } else {
                    //没有网络，请求回调
                    onResultListenerAdapter.onNoNetWork(paramsParser, ErrorCode.ERR_NO_NETWORK);
                }
            }
        }
        return null;

    }
}